/**
 * @Project : ProgrammingExercises
 * @Package : Leetcode
 * @File : Q0203.java
 * @Author : WangRuoyu
 * @Date 2022/12/26 17:59
 */

package leetcode;

import util.LinkList;
import util.ListNode;

public class Q0203 {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return head;
        }

        while (head.val == val) {
            head = head.next;
            if (head == null) {
                return head;
            }
        }

        ListNode p = head;
        ListNode s = head.next;
        while (s != null) {
            if (s.val == val) {
                p.next = s.next;
                s = p.next;
            } else {
                p = p.next;
                s = s.next;
            }
        }
        return head;
    }

    public static void main(String[] args) {
        Q0203 sl = new Q0203();
        ListNode head = LinkList.createList(new int[]{3, 2, 0, 4});
        int x = 3;
        ListNode p = sl.removeElements(head, x);
        LinkList.printList(p);
    }
}
